package user;

import user.student.StudentManager;

import java.util.Scanner;

public class UserView {

    Scanner scanner = new Scanner(System.in);

    UserManager userManager = new UserManager();
    StudentManager studentManager = new StudentManager();

    public boolean registerView(){
        System.out.println("************请注册************");
        System.out.println("请输入姓名：");
        String username = scanner.next();
        System.out.println("请输入密码");
        String password = scanner.next();

        User user = new User(username,password);
        if (userManager.register(user)){
            System.out.println("注册成功请登录！");
            return loginView();
        }
        System.out.println("注册失败！");
        return false;
    }

    public boolean loginView(){
        if (ifUserIsNull()){
            return registerView();
        }
        System.out.println("************请登录************");
        System.out.println("请输入姓名：");
        String username = scanner.next();
        System.out.println("请输入密码");
        String password = scanner.next();

        if ( userManager.login(username, password)){
            System.out.println("登录成功！");
            System.out.println("*************************************************************");
            studentManager.menu();
        }
        System.out.println("登录失败！");
        return false;
    }

    private boolean ifUserIsNull() {
        return userManager.isUserNull();
    }

    public void menuView(){
        System.out.println("*****************************欢迎使用学生信息管理系统*****************************");
        while (true){
            System.out.println("请输入：1、登录\t 2、注册\t 3、退出\t");
            int index = scanner.nextInt();
            switch (index){
                case 1:
                    loginView();
                    break;
                case 2:
                    registerView();
                    break;
                case 3:
                    return;
                default:
                    System.out.println("输入不合法！");

            }
        }


    }


}
